import importlib
from argparse import ArgumentParser

from django.core import serializers
from django.core.management import BaseCommand
from django.db import connection


class Command(BaseCommand):
    def add_arguments(self, parser):
        assert isinstance(parser, ArgumentParser)
        parser.add_argument("-m",
                            type=str,
                            help="add message to print",
                            required=True)

    def handle(self, *args, **kwargs):
        # cursor = connection.cursor()
        # cursor.execute("select * from " + kwargs.pop("m"))
        # for d in cursor.fetchall():
        #     print(d)
        import os
        model_name = kwargs.pop("m")
        models = importlib.import_module(".models", "IM")
        model = getattr(models, model_name)
        print(serializers.serialize("json", model.objects.all()))

